﻿using System;
using System.Collections;
using System.Reflection;
using System.Windows;
using System.ComponentModel;
#if !WinRT
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Interactivity;
using LogoFX.Core;
using EventTrigger = System.Windows.Interactivity.EventTrigger;
#else
using LogoFX.Core;
using Windows.UI.Interactivity;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using EventTrigger = Windows.UI.Interactivity.EventTrigger;
#endif


namespace LogoFX.Infra.Interactivity
{
#if WinRT
    [ContentProperty(Name="Value")]
#else
    [ContentProperty("Value")]
#endif
    [Obsolete("use Microsoft.Expression.Interactivity.Core.ChangePropertyAction where possible")]
    public class TargetedSetPropertyAction
#if WP7
        : BindableTargetedTriggerAction<FrameworkElement>
#else
        : TargetedTriggerAction<object>
#endif
    {
        public static readonly DependencyProperty PropertyNameProperty =
            DependencyProperty.Register("PropertyName", typeof(string), typeof(TargetedSetPropertyAction),
            new PropertyMetadata(null));

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(TargetedSetPropertyAction),
            new PropertyMetadata(null));

#region Properties

        // it might be possible to de
        //public Binding TargetBinding { get; set; }

        
        public string PropertyName
        {
            get { return Convert.ToString(GetValue(PropertyNameProperty)); }
            set { SetValue(PropertyNameProperty, value); }
        }

        
#if WP7
        [TypeConverter(typeof(nRoute.Components.TypeConverters.ConvertFromStringConverter))]
#endif
#if (!WP7)
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
#endif
        public Object Value
        {
            get { return GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

#if WP7

        
#if (!WP7)
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
#endif
        public Binding ValueBinding
        {
            get { return GetBinding(ValueProperty); }
            set { SetBinding<object>(ValueProperty, value); }
        }

#endif

#endregion

#region Trigger Related

        protected override void Invoke(object parameter)
        {
            if (string.IsNullOrEmpty(PropertyName) || Target == null) return;
            
            // we set the value
            var propertyInfo = Target.GetType().GetProperty(PropertyName);
            if (propertyInfo == null || !propertyInfo.CanWrite) return;

            // else we convert and set the value
            var value = Value != null ? ConverterHelper.ConvertToType(Value, propertyInfo.PropertyType) : null;
            propertyInfo.SetValue(Target, value, null);
        }

#endregion

    }
}
